package cmpe273.proj1;


import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

import cmpe273.proj1.dao.CourseDAO;
import cmpe273.proj1.dao.DAOFactory;
import cmpe273.proj1.dao.InstructorDAO;
import cmpe273.proj1.dao.StudentDAO;
import cmpe273.proj1.dao.UserDAO;
import cmpe273.proj1.model.Course;
import cmpe273.proj1.model.Instructor;
import cmpe273.proj1.model.Person;
import cmpe273.proj1.model.Student;

/**
 * 
 * 
 * 
 * @author Rohit
 *
 */
@WebService


public class university_WS {

	private static DAOFactory factory = null;
	private static CourseDAO courseDAO = null;
	private static InstructorDAO instructorDAO = null;
	private static StudentDAO studentDAO = null;
	private static UserDAO userDAO = null;
	
	public university_WS(){
		factory = DAOFactory.getInstance("school.jdbc");
		//factory = DAOFactory.getInstance("school.jndi");
		courseDAO = factory.getCourseDAO();
		instructorDAO = factory.getInstructorDAO();
		studentDAO = factory.getStudentDAO();
		userDAO = factory.getUserDAO();
	}
	
	// SignUp function will be used for requirement of creating student and instructor 
	public String createStudent(String id, String firstName, String lastName, String address, String city, String state, String zipcode){
		String result = "";

			Person person = new Person(lastName, firstName, address, city, state, zipcode);
			Student student = new Student(null, id, person, null);
			result = student.validate();
			if(result.equals("")) {
					try {
							studentDAO.create(student);
							result = "Success";
							userDAO.create(student, id);
						} catch(Exception e) {
						return "error";
					}
			}
			else
				return result;
		return result;
		}
	
	public String createInstructor(String userId, String department, String location, String officeHours, String firstName, String lastName, String address, String city, String state, String zipcode){
		String result = "";
		if(userId == null || userId.length() != 11){
			result = "error : Enter the valid instructorId";
		}
		else{
			Person person = new Person(lastName, firstName, address, city, state, zipcode);
			Instructor instructor = new Instructor(null, userId, department, officeHours, location, person, null);
			result = instructor.validate();
			if(result.equals("")){
				try {
						instructorDAO.create(instructor);
						result = "Success";
						userDAO.create(instructor, userId);
					} catch(Exception e) {
					return e.getMessage();
				}
			}
			else
				return result;
		
		}
		return result;
		
	}
	
	public String logIn(String userId, String pwd){
		String result = "";
		String Id="";
		if(userId == null){
			result = "error : Enter the valid instructorId";
		}
		else if(userId.length() != 11){
			if(!userId.equals("admin")){
				result = "error : Enter the valid instructorId";
			}
			else{
				try{
					result = userDAO.login(userId, pwd);
					result = userDAO.verifyPermissions(result);
				} catch(Exception e){
					result = "error: Wrong password";
				}
			}
			
		}
		else{
			try{
				result = userDAO.login(userId, pwd);
				result = userDAO.verifyPermissions(result);
			} catch(Exception e){
				result = "error: Wrong Username / password";
			}
		}
		return result;   //  format   {S or I}{ID}
	}
	
//	public String logOut(){
//		String result = "";
//		
//		userDAO.logout(sessionId);
//		return result;
//	}
	
	public String create_Course(String courseName, String section, String times, String location){
		String result = "";
		Course course = new Course(null, courseName, section, times, location);
		result = course.validate();
		if(result.equals("")){
			try{
				courseDAO.create(course);
				result = "success";
			}
			catch(Exception e){
				return e.getMessage();
			}
			
		}
		return result;
	}
	
	public String deletePerson(String Spec, String id){  // Spec -> instructor, student, course 
		String result = "";
		if(Spec.equalsIgnoreCase("student"))
			if(id == null || id.length() != 11){
				result = "error : Enter the valid studentId";
			}
			else{
				try{
					Student student = studentDAO.find(id);
					studentDAO.delete(student);		
				} catch(Exception e){
					e.getMessage();
				}
				
			}
		else if(Spec.equalsIgnoreCase("instructor")){
			if(id == null || id.length() != 11){
				result = "error : Enter the valid instructorId";
			}
			else{
				try{
					Instructor instructor = instructorDAO.find(id);
					instructorDAO.delete(instructor);
				} catch(Exception e){
					e.getMessage();
				}
			}
			
		}
			
		return result;		
	}
	
	public String deleteCourse(String name, String section){
		String result = "";
		
		if(name == null ){
				result = "error : Enter the valid courseName";
			}
			else{
				try{
					Course course = courseDAO.find(name, section);
					courseDAO.delete(course);		
				} catch(Exception e){
					e.getMessage();
				}
			}
		return result;
	}
	
	public String[] display_AllStudent(String spec){ // spec should be one of these student, course, person, instructor.
										   // I'm planning to use one display function for all
		ArrayList<String> list = new ArrayList<String>();
		String result = "";
			Student studnt = new Student();
			List<Student> student = studentDAO.search(studnt, true);
			//Student[] students = (Student[]) student.toArray();
			for(int i=0; i<student.size(); i++){
				result = student.get(i).getStudentId() + ">" + student.get(i).getFirstName() + ">" + student.get(i).getLastName() + ">" + student.get(i).getAddress() + ">";
				list.add(i, result);
			}
			String[] stud = new String[list.size()];
			System.out.println("Size of List -->" + list.size());
			list.toArray(stud);
			
			return stud;
	}
	
	public String[] display_AllInstructor(String spec){ // spec should be one of these student, course, person, instructor.
		   // I'm planning to use one display function for all
		ArrayList<String> list = new ArrayList<String>();
		String result = "";
		Instructor inst = new Instructor();
		List<Instructor> instructor = instructorDAO.search(inst, true);
//	Student[] students = (Student[]) student.toArray();
		for(int i=0; i<instructor.size(); i++){
			result = instructor.get(i).getInstructorId() + ">" + instructor.get(i).getFirstName() + ">" + instructor.get(i).getLastName() + ">" + instructor.get(i).getDepartment() + ">" + instructor.get(i).getOfficeHours() + ">" + instructor.get(i).getOfficeLocation() + ">" + instructor.get(i).getAddress() + ">";
			list.add(i, result);
		}
		String[] stud = new String[list.size()];
		list.toArray(stud);
		
		return stud;
	}

	public String[] display_AllCourse(String spec){ // spec should be one of these student, course, person, instructor.
		   // I'm planning to use one display function for all
		ArrayList<String> list = new ArrayList<String>();
		String result = "";
		Course inst = new Course();
		List<Course> instructor = courseDAO.search(inst);
//	Student[] students = (Student[]) student.toArray();
		for(int i=0; i<instructor.size(); i++){
			result = instructor.get(i).getCourseName() + ">" + instructor.get(i).getSection() + ">" + instructor.get(i).getLocation() + ">" + instructor.get(i).getTimes() + ">";
			list.add(i, result);
		}
		String[] stud = new String[list.size()];
		list.toArray(stud);
		
		return stud;
	}
	
	public String[] display_AllPeople(String spec){ // spec should be one of these student, course, person, instructor.
		   // I'm planning to use one display function for all
		ArrayList<String> list = new ArrayList<String>();
		String result = "";
		
		Student studnt = new Student();
		List<Student> student = studentDAO.search(studnt, true);
		//Student[] students = (Student[]) student.toArray();
		
		int i=0;
		for(i=0; i<student.size(); i++){
			result = student.get(i).getStudentId() + ">" + student.get(i).getFirstName() + ">" + student.get(i).getLastName() + ">" + student.get(i).getAddress() + ">";
			list.add(i, result);
		}
		int j =0;
		Instructor inst = new Instructor();
		List<Instructor> instructor = instructorDAO.search(inst, true);
		//Student[] students = (Student[]) student.toArray();
		System.out.println("i - " + i);
		System.out.println("j - " + j);
		while(j<instructor.size()){
			result = instructor.get(j).getInstructorId() + ">" + instructor.get(j).getFirstName() + ">" + instructor.get(j).getLastName() + ">" + instructor.get(j).getAddress() + ">";
			list.add(i, result);
			System.out.println("i - " + i);
			System.out.println("j - " + j);
			i++;
			j++;
		}
		
		String[] people = new String[i];
		list.toArray(people);
		
		return people;
	}
	
	public String updateInstructor(String id, String department, String location, String officeHours, String firstName, String lastName, String address, String city, String state, String zipcode){
		String result = "";
			Person person = new Person(lastName, firstName, address, city, state, zipcode);
			Instructor instructor = new Instructor(null, id, department, officeHours, location, person, null);
			result = instructor.validate();
			if(result.equals(""))
			instructorDAO.update(instructor);
			return result;
	}
	
	
	public String updateStudent(String id, String firstName, String lastName, String address, String city, String state, String zipcode){
		String result = "";
			Person person = new Person(lastName, firstName, address, city, state, zipcode);
			Student student = new Student(null, id, person, null);
			try{
				result = student.validate();
				System.out.println("Validate result");
				if(result.equals(""))
				studentDAO.update(student);
				result = "success";
			} catch(Exception e){
				return e.getMessage();
				//return "Error: Could not update the value";
			}
			
			return result;
	}
	
	
	public String searchCourse(String courseName, String section, String timing, String location){ // Search by attribute
		// this function would return number of courses for that person
		// and number of students enrolled for that course
		String result = "";
		String combine = "";
		Course course = new Course(null, courseName, section, timing, location);
		try{
			List<Course> courseResult = courseDAO.search(course);
			int i=0;
			for(Course c : courseResult){
				result =  result + c.getCourseName() + "%" + c.getSection() + "%" + c.getLocation() + "%" + c.getTimes();
				result = result + "=";
				System.out.println("result from webservice -> " + courseResult.get(i).getCourseName());
//	CHANGE HERE
			}
		} catch(Exception e){
			e.getMessage();
		} finally {
			System.out.println("result in WS - " + result);
			return result;
		}
	}

	public String searchStudent(String studentId, String firstName, String lastName, String address, String city, String state, String zipcode){
		String result = "";
		Person person = new Person(lastName, firstName, address, city, state, zipcode);
		Student course = new Student(null, studentId, person, null);
		try{
			List<Student> courseResult = studentDAO.search(course, false);
			for(Student c : courseResult){
				result = result + ">" + c.getStudentId() + ">" + c.getFirstName() + ">" + c.getLastName() + ">" + c.getAddress() + ">" + c.getCity() + ">" + c.getState() + ">" + c.getZipCode();
				if(c.getEnrolledCourses().size()>0){
					for(int i=0; i<c.getEnrolledCourses().size(); i++)
						result = result + "%" + c.getEnrolledCourses().get(i).getCourseName() +  "-" + c.getEnrolledCourses().get(i).getSection();
				}
				result += "=";
			}
			System.out.println("result in WS -> " + result);
		} catch(Exception e){
			result = e.getMessage();
		} finally {
			return result;
		}
	}
	
	public String searchInstructor(String instructorId, String firstName, String lastName, String department, String officeHours, String address, String city, String state, String zipcode){
		String result="";
		Person person = new Person(lastName, firstName, address, city, state, zipcode);
		Instructor course = new Instructor(null, instructorId, department, officeHours, "", person, null);
		try{
			List<Instructor> courseResult = instructorDAO.search(course, false);
			for(Instructor c : courseResult){
				result = result +c.getInstructorId() + ">" + c.getFirstName() + ">" + c.getLastName() + ">" + c.getDepartment()+ ">" + c.getAddress() + ">" + c.getCity() + ">" + c.getState() + ">" + c.getZipCode()+">"+c.getOfficeLocation()+">"+c.getOfficeHours();
				if(c.getTaughtCourses().size()>0){
					for(int i=0; i<c.getTaughtCourses().size(); i++)
						result = result + "%" + c.getTaughtCourses().get(i).getCourseName() +  "-" + c.getTaughtCourses().get(i).getSection();
				}
				result += "=";
			}
		} catch(Exception e){
			result = e.getMessage();
		} finally {
			return result;
		}
	}
	
	public String[] searchLoginStudent(String studentId){
		String[] result = new String[2];
		
		Person person = new Person();
		Student course = new Student(null, studentId, person, null);
		try{
		List<Student> courseResult = studentDAO.search(course, false);
		System.out.println(courseResult.size());
		for(Student c : courseResult){
				result[0] = c.getStudentId() + ">" + c.getFirstName() + ">" + c.getLastName() + ">" + c.getAddress() + ">" + c.getCity() + ">" + c.getState() + ">" + c.getZipCode() + "=";
				if(c.getEnrolledCourses().size()>0){
					for(int i=0; i<c.getEnrolledCourses().size(); i++)
						if(i==0)
						result[1] = c.getEnrolledCourses().get(i).getCourseName() +  ">" + c.getEnrolledCourses().get(i).getSection() + "=";
						else
							result[1] = result[1] + c.getEnrolledCourses().get(i).getCourseName() +  ">" + c.getEnrolledCourses().get(i).getSection() + "=";
							
				}
				
			}
		
		System.out.println("0-->"+result[0]);
		System.out.println("1-->"+result[1]);
		} catch(Exception e){
			result[0] = e.getMessage();
		} finally 
		{
		return result;
		}
		
	}
	
	
	public String[] searchAllStudent(String studentId, String firstName, String lastName, String address, String city, String state, String zipcode){
		String[] result = new String[2];
		Person person = new Person(lastName, firstName, address, city, state, zipcode);
		Student course = new Student(null, studentId, person, null);
		try{
		List<Student> courseResult = studentDAO.search(course, true);
		if(courseResult.size() == 1){
			for(Student c : courseResult){
				result[0] = c.getStudentId() + ">" + c.getFirstName() + ">" + c.getLastName() + ">" + c.getAddress() + ">" + c.getCity() + ">" + c.getState() + ">" + c.getZipCode();
				if(c.getEnrolledCourses().size()>0){
					for(int i=0; i<c.getEnrolledCourses().size(); i++)
						result[0] = result[0] + ">" + c.getEnrolledCourses().get(i).getCourseName() +  ">" + c.getEnrolledCourses().get(i).getSection() + ">" + c.getEnrolledCourses().get(i).getTimes() + ">" + c.getEnrolledCourses().get(i).getLocation();
				}
				result[0] += "=";
			}
		}
		else{
		for(Student c : courseResult){
			result[0] = result[0] + ">" + c.getStudentId() + ">" + c.getFirstName() + ">" + c.getLastName() + ">" + c.getAddress() + ">" + c.getCity() + ">" + c.getState() + ">" + c.getZipCode();
			if(c.getEnrolledCourses().size()>0){
				for(int i=0; i<c.getEnrolledCourses().size(); i++)
					result[0] = result[0] + ">" + c.getEnrolledCourses().get(i).getCourseName() +  ">" + c.getEnrolledCourses().get(i).getSection() + ">" + c.getEnrolledCourses().get(i).getTimes() + ">" + c.getEnrolledCourses().get(i).getLocation();
			}
			result[0] += "=";
		}
		}
		} catch(Exception e){
			result[0] = e.getMessage();
		} finally 
		{
		return result;
		}
		
	}
	
	public String[] searchLoginInstructor(String instructorId){
		String[] result = new String[2];
		
		Person person = new Person();
		Instructor course = new Instructor(null, instructorId, "", "", "", person, null);
		try{
		List<Instructor> courseResult = instructorDAO.search(course, false);
		System.out.println(courseResult.size());
		for(Instructor c : courseResult){
				result[0] = c.getInstructorId() + ">" + c.getFirstName() + ">" + c.getLastName() + ">" + c.getDepartment() + ">" + c.getOfficeHours() + ">" + c.getAddress() + ">" + c.getCity() + ">" + c.getState() + ">" + c.getZipCode() + ">" + c.getOfficeLocation() + "=";
				if(c.getTaughtCourses().size()>0){
					for(int i=0; i<c.getTaughtCourses().size(); i++)
						if(i==0)
						result[1] = c.getTaughtCourses().get(i).getCourseName() +  ">" + c.getTaughtCourses().get(i).getSection() + "=";
						else
							result[1] = result[1] + c.getTaughtCourses().get(i).getCourseName() +  ">" + c.getTaughtCourses().get(i).getSection() + "=";
				}
				
			}
		
		System.out.println("0-->"+result[0]);
		System.out.println("1-->"+result[1]);
		} catch(Exception e){
			result[0] = e.getMessage();
		} finally 
		{
		return result;
		}
	}
	
	
	
	public String searchStudentEnrolledIn(String coursName, String Section){
		String result = "";
		Course course = new Course(null, coursName, Section, null, null);
		List<Student> student = studentDAO.searchStudentsEnrolledIn(course);
		ArrayList<String> list = new ArrayList<String>();
		System.out.println("Size of List -->" + student.size());
		if(student.size()!=0){
		for(int i=0; i<student.size(); i++){
			result = student.get(i).getStudentId() + ">" + student.get(i).getFirstName() + ">" + student.get(i).getLastName() + ">" + student.get(i).getAddress() + ">";
			System.out.println(result);
			list.add(i, result);
		}
		String[] stud = new String[list.size()];
		System.out.println("Size of List -->" + list.size());
		result = "";
		result = list.get(0);
		for(int i=1; i < list.size(); i++){
			result = result + "=" + list.get(i);
		}
		}
//		list.toArray(stud);
//		System.out.println("in search student server");
		return result;
	}
	
	
	public String enrollStudent(String courseName, String Section, String studentId){ 
																			// list of courses and sections with '?' delimiter
		String result = "";
		try{
		Student student = studentDAO.find(studentId);
		Course course = courseDAO.find(courseName, Section);
		studentDAO.addCourse(student, course);
		}catch(Exception e){
			return " No course found";
		}
						
		return result;
	}
	
	public String unenrollStudent(String courseName, String Section, String studentId){
		String result = "";
		Student student = studentDAO.find(studentId);
		Course course = courseDAO.find(courseName, Section);
		studentDAO.removeCourse(student, course);	
		return result;
	}
	
	public String assignInstructor(String courseName, String Section, String instructorId){
		String result = "";
		
		Instructor instructor = instructorDAO.find(instructorId);
		Course course = courseDAO.find(courseName, Section);
			instructorDAO.addCourse(instructor, course);	
		
		
		return result;
	}
	
	public String replaceInstructor(String courseName, String Section, String instructorId){
		String result = "";
		Instructor instructor = instructorDAO.find(instructorId);
		
		Course course = courseDAO.find(courseName, Section);
		instructorDAO.removeCourse(instructor, course);	
		
		return result;
	}

	public String updateCourse(String name, String Section, String meetingTime, String Location){
		String result = "";
		
			Course course = new Course(null, name, Section, meetingTime, Location);
			result = course.validate();
			if(result.equals(""))
			courseDAO.update(course);
		
		return result;
		
	}
	
}









